/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.Excel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.primefaces.component.fileupload.FileUpload;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author vt
 */
public class UploadFileExcel {
   private String malop;
   FileUpload bindingFileUpload;

    public FileUpload getBindingFileUpload() {
        return bindingFileUpload;
    }

    public void setBindingFileUpload(FileUpload bindingFileUpload) {
        this.bindingFileUpload = bindingFileUpload;
    }

    public String getMalop() {
        return malop;
    }

    public void setMalop(String malop) {
        this.malop = malop;
    }
      public  void uploadfile(FileUploadEvent event) {  
              System.out.println("CHAY VAO UPLOAD FILE " +event.getComponent().toString());
          FileUpload component  = (FileUpload) event.getComponent();
             String k  =  component.getDescription();
             System.out.println("CHAY_______FILE " + k);
        ExternalContext extContext=FacesContext.getCurrentInstance().getExternalContext();
            File result1 = new File(getUrlBase() + "/Thom");
            result1.mkdir();
            System.out.println("Ton Tai File : " + result1.exists());
File result = new File(extContext.getRealPath("//WEB-INF//files//" +event.getFile().getFileName() ));
        try {

FileOutputStream fileOutputStream = new FileOutputStream(result);

 

byte[] buffer = new byte[6124];

 

int bulk;

InputStream inputStream = event.getFile().getInputstream();

while (true) {

bulk = inputStream.read(buffer);

if (bulk < 0) {

break;

}

fileOutputStream.write(buffer, 0, bulk);

fileOutputStream.flush();

}

fileOutputStream.close();

inputStream.close();

FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName()

+ " is uploaded.");

FacesContext.getCurrentInstance().addMessage(null, msg);

} catch (IOException e) {

e.printStackTrace();

 

FacesMessage error = new FacesMessage(FacesMessage.SEVERITY_ERROR,
"The files were not uploaded!", "");
FacesContext.getCurrentInstance().addMessage(null, error);

}      

}   
  public boolean isShowUpload(){
      System.out.println("Show UPDOAD " + malop);
      if(malop != null){
          return !malop.equals("");
      }
      return false;
  }
    private String getUrlBase() {
      return FacesContext.getCurrentInstance().getExternalContext().getRealPath("//upload");
    }
}
